/* 
 * File:   GameState.cpp
 * Author: noe
 * 
 * Created on 5 octobre 2013, 21:40
 */

#include "GameState.h";
#include "input.h"
#include "LobbyState.h"
#include "Texture.h"

std::vector<std::string> messages;
Texture* t;
Texture* t1;

int angleX;
int angleZ;
int oldX;
int oldY;

GameState::GameState() {
}

GameState::GameState(const GameState& orig) {
}

GameState::~GameState() {
}

void GameState::create()
{
	t = new Texture("resources/metal.jpg");
	t1 = new Texture("resources/penis.png");
	
	
}

void GameState::render(float delta)
{
//	 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// 
//    glMatrixMode( GL_MODELVIEW );
//    glLoadIdentity( );
// 
//    gluLookAt(3,4,2,0,0,0,0,0,1);
// 
//    glRotated(angleZ,0,0,1);
//    glRotated(angleX,1,0,0);
// 
//    glBegin(GL_QUADS);
// 
//    glColor3ub(255,0,0); //face rouge
//    glVertex3d(1,1,1);
//    glVertex3d(1,1,-1);
//    glVertex3d(-1,1,-1);
//    glVertex3d(-1,1,1);
// 
//    glColor3ub(0,255,0); //face verte
//    glVertex3d(1,-1,1);
//    glVertex3d(1,-1,-1);
//    glVertex3d(1,1,-1);
//    glVertex3d(1,1,1);
// 
//    glColor3ub(0,0,255); //face bleue
//    glVertex3d(-1,-1,1);
//    glVertex3d(-1,-1,-1);
//    glVertex3d(1,-1,-1);
//    glVertex3d(1,-1,1);
// 
//    glColor3ub(255,255,0); //face jaune
//    glVertex3d(-1,1,1);
//    glVertex3d(-1,1,-1);
//    glVertex3d(-1,-1,-1);
//    glVertex3d(-1,-1,1);
// 
//    glColor3ub(0,255,255); //face cyan
//    glVertex3d(1,1,-1);
//    glVertex3d(1,-1,-1);
//    glVertex3d(-1,-1,-1);
//    glVertex3d(-1,1,-1);
// 
//    glColor3ub(255,0,255); //face magenta
//    glVertex3d(1,-1,1);
//    glVertex3d(1,1,1);
//    glVertex3d(-1,1,1);
//    glVertex3d(-1,-1,1);
// 
//    glEnd();
// 
//    glFlush();
//    SDL_GL_SwapBuffers();
//	glClear(GL_COLOR_BUFFER_BIT);
////	glClearColor(1,1,1,1);	
//	glBegin(GL_TRIANGLES);
//	glColor3ub(getMouseX() * 255 / 800,0,0);	glVertex2d(-0.75,-0.75);
//	glColor3ub(0, getMouseY() * 255 / 600,0);	glVertex2d(0,0.75);
//	glColor3ub(0,0,255);	glVertex2d(0.75,-0.75);
//	glEnd();
//	glFlush();
//	SDL_GL_SwapBuffers();
//	
//	glBindTexture(GL_TEXTURE_2D, t->getId());
////	gluLookAt(3,2,3,0,0,0,0,1,0);
//    glBegin(GL_QUADS);
//    glTexCoord2d(0,1);  glVertex2d(0.75,0.75);
//    glTexCoord2d(0,0);  glVertex2d(0.75,0.75);
//    glTexCoord2d(1,0);  glVertex2d(-0.75,0.75);
//    glTexCoord2d(1,1);  glVertex2d(-0.75,0.75);
//	glEnd();
	
	glEnable( GL_TEXTURE_2D );
	
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity( );

    gluLookAt(3,4,5,0,0,0,0,0,1);

    glRotated(angleX,0,0,1);

    //LE SOL
    glBindTexture(GL_TEXTURE_2D, t->getId());
    glBegin(GL_QUADS);
    glColor3ub(255,0,0);
    glTexCoord2i(0,0);
    glVertex3i(-10,-10,-1);
    glColor3ub(0,255,0);
    glTexCoord2i(10,0);
    glVertex3i(10,-10,-1);
    glColor3ub(255,255,0);
    glTexCoord2i(10,10);
    glVertex3i(10,10,-1);
    glColor3ub(255,0,255);
    glTexCoord2i(0,10);
    glVertex3i(-10,10,-1);
    glEnd();

    glColor3ub(0,0,0);

    glFlush();
    SDL_GL_SwapBuffers();

}

void GameState::update(float delta)
{
//	std::cout << "x : " << getMouseX() << "\n";
	
	messages = Game::getGame()->getNetworkManager()->getMessages();
	int i = 0;
	
	while(i < messages.size())
	{
		std::cout << messages[i] << "\n";
		if(messages[i] == "95041")
		{
			Game::getGame()->setCurrentState(new LobbyState());
		}
		i++;
	}
		
	if(isLeftPressed())
	{
		angleX = 1066 - getMouseX();
//		Game::getGame()->setCurrentState(new LobbyState());
	}
}

